<?php
/*
if(isset($_POST['lines']))
{
	$ficnm = $_GET['ficnm'];
	$t_lines = preg_split('/<f><f>/',$_POST['lines']);
	if(is_array($t_lines))
	{
		$fp = fopen(_TEMP_."/".$ficnm,"w");
		foreach($t_lines as $key => $s_lines)
		{
			$t_cells = preg_split('/<f>/',$s_lines);
			$olines = '';
			foreach($t_cells as $cid => $value)
			{
				$value = utf8_decode($value);
				$olines .= $value.';';
			}
			fwrite($fp,"$olines\n");
		}
		 fclose($fp);
	}
	echo "File Transfered";
}
*/
if(isset($_POST['lines']))
{
	$ficnm = $_GET['ficnm'];
	
	// Color of odd number line
	$colorLineOdd = 'ececff';
	// Color of pair number line
	$colorLinePair = 'ffffff';
	// Font of header family
	$headerFont = 'Verdana';
	// Font of header bold
	$headerBold = true;
	// Font of header size
	$headerSize = 10;
	// Font of content family
	$contentFont = 'Verdana';
	// Font of content size
	$contentSize = 10;
	
	$objPHPExcel = new PHPExcel();
	
	// TODO : Set right properties of xlsx output file
	$objPHPExcel->getProperties()->setCreator("")
							 ->setLastModifiedBy("")
							 ->setTitle($ficnm)
							 ->setSubject($ficnm)
							 ->setDescription("")
							 ->setKeywords("excel php hello world")
							 ->setCategory("Test result file");
							 
	$sheet = $objPHPExcel->getActiveSheet();
	
	// Get width of columns
	if(isset($_POST['width']))
	{
		$tableWidth = preg_split("/<f>/",$_POST['width']);
	}
	
	// Get content on POST lines
	$t_lines = preg_split("/<f><f>/",$_POST['lines']);
	if(is_array($t_lines))
	{
		$firstHeadCell = 'A1';
		$lastHeadCell = '';
		$firstContentCell = 'A2';
		$lastContentCell;
		
		// Loop on lines
		foreach($t_lines as $key => $s_lines)
		{
			// $num : line
			// $alpha : column
			$num = $key + 1;
			$alpha = 'A';
			
			$t_cells = preg_split('/<f>/',$s_lines);
			// Loop on cells
			foreach($t_cells as $cid => $value)
			{
				if($num == 1)
				{
					// Set width of column proportionately
					
					if(isset($tableWidth[$cid]))
					{
						$excelCellWidth = number_format(($tableWidth[$cid] / 7.23),'2');
						if($excelCellWidth < 5)
							$excelCellWidth = 5;
						$sheet->getColumnDimension($alpha)->setWidth($excelCellWidth);
					}
					
					// Build a data type array
					$s_value = preg_split("/<x>/",$value);
					$value = $s_value[0];
					if(isset($s_value[1]))
						$t_dtype[] = substr($s_value[1],0,5);
				}
				else
				{
					// Format Numbers
					if($t_dtype[$cid])
					{
						if($t_dtype[$cid] == 'float')
							$value = preg_replace('/,/','',$value);
					}
				}
				
				// Put content in cell
				$sheet->setCellValue($alpha.$num, utf8_decode($value));
				if($cid != (count($t_cells)-1))
					$alpha++;
			}
			
			// Set background color on line
			// Get header last cell
			if($alpha>$lastHeadCell)
				$lastHeadCell = $alpha;
		}
		$lastContentCell = $lastHeadCell.$num;
		$lastHeadCell .= '1';

		// Set line 1 height
		$sheet->getRowDimension('1')->setRowHeight(20);
		// Add header styles
		$sheet->getStyle($firstHeadCell.':'.$lastHeadCell)->applyFromArray( 
				array(
					'font'    => array(
						'bold'      => $headerBold,
						'name'		=> $headerFont,
						'size'		=> $headerSize
					),
					'alignment' => array(
						'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
						'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
					),
					'fill' => array(
			 			'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
			  			'rotation'   => 90,
			 			'startcolor' => array(
			 				'argb' => 'FFFFFFFF'
			 			),
			 			'endcolor'   => array(
			 				'rgb' => 'b8cfee'
			 			)
			 		)
				)
		);
		
		// Set header border
		$sheet->getStyle($firstHeadCell.':'.$lastHeadCell)->applyFromArray(
			array(
				'borders' => array(
					'outline'     => array(
	 					'style' => PHPExcel_Style_Border::BORDER_THIN,
						'color' => array('rgb' => '74b2e2')
	 				)
				)
			)
		);
		
		// Add border and font on content
		$styleThinBlackBorderOutline = array(
			'borders' => array(
				'outline' 	=> array(
					'style' => PHPExcel_Style_Border::BORDER_THIN,
					'color' => array('rgb' => '74b2e2')
				),
			),
			'font'    => array(
				'name'		=> $contentFont,
				'size'		=> $contentSize
			)
		);
		$sheet->getStyle($firstContentCell.':'.$lastContentCell)->applyFromArray($styleThinBlackBorderOutline);
	}
	// TODO : add a good message from db
	echo Msg('FILE',9);
	
	// Create file xlsx
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
	$objWriter->save(_TEMP_.$ficnm);
}
?>